package org.prem.jb.hibernate.onetomany;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.prem.jb.hibernate.onetomany.Address;
import org.prem.jb.hibernate.onetomany.Student;
import org.prem.jb.hibernate.onetomany.Vehicle;

public class OneToManyMain {

	public static void main(String[] args){
		List<Address> addressList = new ArrayList<Address>();
		Student student = new Student();
		student.setName("PremKB1");
		Vehicle vehicle = new Vehicle();
		vehicle.setVehicleName("Indica");
		//vehicle.setStudent(student);
		Vehicle vehicle1 = new Vehicle();
		vehicle1.setVehicleName("VW Polo");
		//vehicle1.setStudent(student);
		List<Vehicle> vehicleList = new ArrayList<Vehicle>();
		vehicleList.add(vehicle);
		vehicleList.add(vehicle1);
		student.setVehicle(vehicleList);
/*		Address address = new Address();
		address.setDoorNo("42B");
		address.setStreet("2nd Old Murugan Talkies Street Palanganatham1");
		address.setZipcode("625003");
		addressList.add(address);
		Address address1 = new Address();
		address1.setDoorNo("G6");
		address1.setStreet("186 Velachery main road1"); 
		address1.setZipcode("600059");
		addressList.add(address1);
		student.setAddressList(addressList);*/
		Address collegeAddress = new Address();
		collegeAddress.setDoorNo("10B");
		collegeAddress.setStreet("Perundurai");
		collegeAddress.setZipcode("638003");
		student.setCollegeAddress(collegeAddress);
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(student);
		session.flush();
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}
}
